console.log("zjy   content-script.js");
document.addEventListener('DOMContentLoaded', function () {
    // 注入自定义JS
    injectCustomJs();
});

function showtoast(e) {
    let lock_out = false, n;
    let h = $(window).height(), w = $(window).width(), leftz = w / 2 - 110;
    $(`<div id='zjy'></div>`).css({
        width: '200px',
        position: 'fixed',
        left: leftz + 'px',
        background: 'rgba(0,0,0,0.5)',
        zIndex: '999',
        textAlign: 'center',
        padding: '20px',
        borderRadius: '10px',
        display: 'none',
        fontSize: "18px"
    }).appendTo("body").on('mouseenter', e => {
        lock_out = true
    }).on('mouseleave', e => {
        lock_out = false;
    }).append($('<p>').text(e).css({
        color: 'white'
    })).css({
        top: e => {
            return (h / 2 - $("#zjy").innerHeight() / 2) + 'px';
        },
    }).fadeToggle(500);
    setTimeout(() => {
        if (lock_out) {
            n = setInterval(() => {
                if (!lock_out) {
                    clearInterval(n);
                    $("#zjy").fadeToggle(500, () => {
                        $('#zjy').remove();
                    })
                }
                console.log(lock_out, n, "uuuu")
            }, 500);
        } else {
            $("#zjy").fadeToggle(500, () => {
                $('#zjy').remove();
            })
        }
    }, 1500)

}
//接受来自inject.js
window.addEventListener("message", function (e) {
    //console.log(e.data, "content.js:  get from inject");
    if (e.data.translate) {
        send("translate", e.data.translate).then(res=>{
            console.log('content.js:  recieve from inject:' + res);
            showtoast(res)
        });
    }
}, false);
//接收来自background.js/popup.js
chrome.runtime.onMessage.addListener(function (req, sender, back) {
    // console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");
    console.log(req, "content.js: recieve:"+req)
    let arr = ['getbookmarks']
    for (let k in arr) {
        if (req[arr[k]]) {
            console.log('req.' + arr[k] + ":" + req[arr[k]], "赛选");
        }
    }
});
//发送至backg.js/popup.js
function send(key, value) {
    return new Promise((res, err) => {
        chrome.runtime.sendMessage({ [key]: value }, function (res2) {
            console.log('content.js: send'+{ [key]: value }+';back:' + res2);
            res(res2);
        });
    })
}
function getbookmarks() {
    chrome.runtime.sendMessage({ bookmarks: true }, function (res) {
        console.log(res, "book")
    });
}
// 向页面注入JS
function injectCustomJs(jsPath) {
    jsPath = jsPath || 'js/inject.js';
    var temp = document.createElement('script');
    temp.setAttribute('type', 'text/javascript');
    // 获得的地址类似：chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject.js
    temp.src = chrome.extension.getURL(jsPath);
    /*     temp.onload = function () {
            // 放在页面不好看，执行完后移除掉
            this.parentNode.removeChild(this);
        }; */
    document.body.appendChild(temp);
}